package com.ruoyi.erecord.vehicles.service.impl;

import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.BeanCopyUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.erecord.vehicles.domain.VehiclesImportAttach;
import com.ruoyi.erecord.vehicles.domain.bo.VehiclesImportAttachBo;
import com.ruoyi.erecord.vehicles.domain.vo.VehiclesImportAttachVo;
import com.ruoyi.erecord.vehicles.mapper.VehiclesImportAttachMapper;
import com.ruoyi.erecord.vehicles.service.IVehiclesImportAttachService;

import cn.hutool.core.bean.BeanUtil;
import lombok.RequiredArgsConstructor;

/**
 * 车辆导入附件Service业务层处理
 *
 * @author cc
 * @date 2023-04-18
 */
@RequiredArgsConstructor
@Service
public class VehiclesImportAttachServiceImpl implements IVehiclesImportAttachService {

    private final VehiclesImportAttachMapper baseMapper;

    /**
     * 查询车辆导入附件
     */
    @Override
    public VehiclesImportAttachVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询车辆导入附件列表
     */
    @Override
    public TableDataInfo<VehiclesImportAttachVo> queryPageList(VehiclesImportAttachBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<VehiclesImportAttach> lqw = buildQueryWrapper(bo);
        Page<VehiclesImportAttachVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询车辆导入附件列表
     */
    @Override
    public List<VehiclesImportAttachVo> queryList(VehiclesImportAttachBo bo) {
        LambdaQueryWrapper<VehiclesImportAttach> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<VehiclesImportAttach> buildQueryWrapper(VehiclesImportAttachBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<VehiclesImportAttach> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getBatchNo()), VehiclesImportAttach::getBatchNo, bo.getBatchNo());
        lqw.eq(bo.getProcessStatus() != null, VehiclesImportAttach::getProcessStatus, bo.getProcessStatus());
        return lqw;
    }

    /**
     * 新增车辆导入附件
     */
    @Override
    public Boolean insertByBo(VehiclesImportAttachBo bo) {
        VehiclesImportAttach add = BeanUtil.toBean(bo, VehiclesImportAttach.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改车辆导入附件
     */
    @Override
    public Boolean updateByBo(VehiclesImportAttachBo bo) {
        VehiclesImportAttach update = BeanUtil.toBean(bo, VehiclesImportAttach.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(VehiclesImportAttach entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除车辆导入附件
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

	@Override
	public Boolean batchInsertByBo(List<VehiclesImportAttachBo> volist) {
		List<VehiclesImportAttach> list = BeanCopyUtils.copyList(volist , VehiclesImportAttach.class);
		return baseMapper.insertBatch(list);
	}
}
